package com.example.chasshelper.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.chasshelper.LoginActivity;
import com.example.chasshelper.R;
import com.example.chasshelper.adapter.ClassroomStuAdapter;
import com.example.chasshelper.dao.ClassroomDao;
import com.example.chasshelper.dao.CourseDao;
import com.example.chasshelper.dao.MessageDao;
import com.example.chasshelper.dao.RegistrationDao;
import com.example.chasshelper.dao.SolutionDao;
import com.example.chasshelper.dao.StudentDao;
import com.example.chasshelper.dao.TeacherDao;
import com.example.chasshelper.dao.TestDao;
import com.example.chasshelper.model.Classroom;
import com.example.chasshelper.model.Student;
import com.example.chasshelper.utils.DaoUtils;

import java.util.ArrayList;
import java.util.List;

public class ClassroomStuFragment extends Fragment implements View.OnClickListener{

    // TODO: Rename parameter arguments, choose names that match
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private View view;
    private Activity mActivity;
    private Context mContext;
    //控件
    private EditText et_classroomId;
    private ImageView iv_search;
    private RecyclerView rv_classroom_stu;
    //数据
    private List<Classroom> classroomList;
    //dao
    private TeacherDao teacherDao= DaoUtils.getTeacherDao();
    private StudentDao studentDao=DaoUtils.getStudentDao();
    private CourseDao courseDao=DaoUtils.getCourseDao();
    private ClassroomDao classroomDao=DaoUtils.getClassroomDao();
    private RegistrationDao registrationDao=DaoUtils.getRegistrationDao();
    private SolutionDao solutionDao = DaoUtils.getSolutionDao();
    private MessageDao messageDao = DaoUtils.getMessageDao();
    private TestDao testDao = DaoUtils.getTestDao();
    //adapter
    private ClassroomStuAdapter classroomStuAdapter;
    public ClassroomStuFragment() {
    }

    // TODO: Rename and change types and number of parameters
    public static ClassroomStuFragment newInstance(String param1, String param2) {
        ClassroomStuFragment fragment = new ClassroomStuFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 绑定布局
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_classroom_stu, container, false);
        }
        initUI();
        initData();
        return view;
    }

    private void initUI() {
        //绑定控件
        rv_classroom_stu = view.findViewById(R.id.rv_classroom_stu);
        et_classroomId = view.findViewById(R.id.et_classroomId);
        iv_search = view.findViewById(R.id.iv_search);
        //点击事件
        iv_search.setOnClickListener(this);
    }

    //接收并传递数据
    private void initData() {
        //接收数据
        mActivity = getActivity();
        mContext = getContext();
        Bundle bundle = this.getArguments();
        Student student = (Student)bundle.getSerializable("student");
        System.out.println("ClassroomStuFragment:"+student);
        //dao获取数据
        classroomList = classroomDao.findByStudentId(student);
        //adapter初始化，并向adapter传递数据
        classroomStuAdapter = new ClassroomStuAdapter(mContext,classroomList,student);
        rv_classroom_stu.setLayoutManager(new LinearLayoutManager(mContext));
        rv_classroom_stu.setAdapter(classroomStuAdapter);
        registerForContextMenu(rv_classroom_stu);
    }

    /*
    * 点击事件
    * */
    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        String classId = et_classroomId.getText().toString();
        //测试
        System.out.println("课室编号："+classId);

        switch (view.getId()) {
            case R.id.iv_search://前往搜索结果页面
                Toast.makeText(mContext,"前往搜索结果页面",Toast.LENGTH_SHORT).show();
                break;
        }
    }





/**********************************************************************************************************************************************/

    // 这个方法是创建点击以后显示的布局条目的
    @Override
    public void onCreateContextMenu(@NonNull ContextMenu menu, @NonNull View v, @Nullable ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getActivity().getMenuInflater().inflate(R.menu.commen_menu,menu);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.delete) {
            //Classroom classroom =classroomList.get(classsroom.getPosition());// 通过adapter的getposition获得当前点击的是第几条,针对某条做相应操作.
            Toast.makeText(mContext,"删除", Toast.LENGTH_SHORT).show();
        }
        return super.onContextItemSelected(item);
    }
}